home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / delphi.swg / 0224_Re: Add most recent files to menu.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1997-03-04  |  2.5 KB  |  84 lines

  1. {
  2. > How do you add the most recent files accessed to the file menu?
  3.  
  4. Of course, there's no one answer to this question, but here are some
  5. excerpts from my implementation of that feature, clipped from a text
  6. editor I wrote.
  7.  
  8. First, I subclassed TStringList to hold the list of recent files.
  9. }
  10.  
  11. type
  12.   TRecentFileList = class(TStringList)
  13.   public
  14.     constructor Create;
  15.     destructor Destroy; override;
  16.     function Add(const S: String): Integer; override;
  17.     procedure Remove(const s: String);
  18.   end;
  19.  
  20. Create and Destoy mostly involve reading and writing the list to the
  21. registry, so that the recent file list will be persistent. Add and
  22. Remove are shown below.
  23.  
  24. function TRecentFileList.Add(const S: String): Integer;
  25. begin
  26.   Result := IndexOf(s);
  27.   if (Result = -1) then begin
  28.     if Count >= MAX_RECENT_FILES then Delete(MAX_RECENT_FILES - 1);
  29.     Insert(0, s); Result := 0;
  30.   end;
  31. end;
  32.  
  33. procedure TRecentFileList.Remove(const s: String);
  34. var
  35.   i: Integer;
  36. begin
  37.   i := IndexOf(s);
  38.   if i >= 0 then Delete(i);
  39. end;
  40.  
  41. The main form contains a TRecentFileList called recentFileList. When
  42. the program closes a file it adds it to this list; When it opens
  43. one, it removes it. (As shown, the TRecentFileList is smart
  44. enough not to add a file twice, or to try and delete a non-existant
  45. file.) The OnClick handler for the main menu's "File" menu item,
  46. FileMenuClick, creates a TMenuItem for each recent file in the
  47. list and adds it to the TMenuItem named FileReopenItem before it
  48. opens the menu. Thus the names of the recent files appear in a
  49. submenu to a menu item captioned "Reopen".
  50.  
  51. procedure TMainForm.FileMenuClick(Sender: TObject);
  52. var
  53.   i: Integer;
  54.   mi: TMenuItem;
  55. begin
  56.   if recentFileList.Count = 0 then FileReopenItem.Enabled := False
  57.   else begin
  58.     FileReopenItem.Enabled := True;
  59.     for i := FileReopenItem.Count - 1 downto 0 do
  60.       FileReopenItem.Delete(i);
  61.     for i := 0 to recentFileList.Count - 1
  62.     do begin
  63.       mi := TMenuItem.Create(Self);
  64.       mi.Caption := recentFileList[i];
  65.       mi.OnClick := FileReopen;
  66.       FileReopenItem.Add(mi);
  67.     end;
  68.   end;
  69. end;
  70.  
  71. The recent file menu items each have the procedure FileReopen as
  72. their OnClick handler. When the user clicks one of the recent file
  73. menu items, this procedure uses the caption of the clicked item to
  74. determine what file to reopen.
  75.  
  76. procedure TMainForm.FileReopen(Sender: TObject);
  77. var
  78.   fileName: String;
  79. begin
  80.   fileName := (Sender as TMenuItem).Caption;
  81.   CreateMemoPage.LoadFromFile(fileName);
  82.   recentFileList.Remove(fileName);
  83. end;
  84.